package com.sinsz.c.context;

import com.sinsz.c.annotation.EnableBxfs;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @EnableBxfs 注解实现
 * @author chenjianbo
 * @date 2017/11/23
 */
public class BxfsRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        AnnotationAttributes attributes = AnnotationAttributes.fromMap(
                importingClassMetadata.getAnnotationAttributes(EnableBxfs.class.getName())
        );
        List<String> list = new ArrayList<>();
        String defaultPackage = "com.sinsz.c";
        String[] packages = attributes.getStringArray("value");
        if (packages != null && packages.length > 0) {
            list = Arrays.stream(packages)
                    .filter(str -> str != null && !"".equals(str.trim()) && !defaultPackage.equals(str))
                    .collect(Collectors.toList());
        }
        list.add(defaultPackage);
        BxfsScanner scanner = new BxfsScanner(registry);
        scanner.setScanPackages(list.toArray(new String[list.size()]));
        boolean enableLogger = attributes.getBoolean("enableLogger");
        if (enableLogger) {
            String loggerPath = attributes.getString("loggerPath");
            String loggerToken = attributes.getString("loggerToken");
            String http = "",
                    filePath = "",
                    httpLogo = "http://",
                    httpsLogo = "https://";
            if (loggerPath != null && !"".equals(loggerPath.trim())) {
                boolean isHttps = loggerPath.contains(httpsLogo) && loggerPath.indexOf(httpsLogo) == 0;
                boolean isHttp = loggerPath.contains(httpLogo) && loggerPath.indexOf(httpLogo) == 0;
                if (isHttp || isHttps) {
                    http = loggerPath;
                    BxfsLoggerUtil.instance().init(http, filePath, loggerToken);
                    return;
                }
            }
            //默认记录方式
            Date date = new Date();
            String year = String.format("%tY",date);
            String month = String.format("%tm",date);
            String day = String.format("%td",date);
            filePath = System.getProperty("user.dir")
                    + File.separator + "logs"
                    + File.separator + year
                    + File.separator + month
                    + File.separator + day
                    + File.separator;
            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            BxfsLoggerUtil.instance().init(http, filePath, loggerToken);
        }
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
    }

    private class BxfsScanner extends ClassPathBeanDefinitionScanner {

        BxfsScanner(BeanDefinitionRegistry registry) {
            super(registry);
        }

        void setScanPackage(String basePackages) {
            setScanPackages(basePackages);
        }

        void setScanPackages(String... basePackages) {
            super.doScan(basePackages);
        }
    }
}